<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/9
 * Time: 11:24
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Constant\ErrorConstant;
use App\Service\Api\GoodsService;
use Illuminate\Support\Facades\Validator;

class GoodsController extends BaseController
{
    protected $service;

    public function __construct(Request $request, GoodsService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 商品列表页
     * @return \Illuminate\Http\JsonResponse
     */
    public function listGoods()
    {
        $validator  =   Validator::make($this->request->input(), [
            'cat_id'    =>  'string|exists:mini_category,unique_id',
            'page'      =>  'integer',
            'name'      =>  'string|in:real_sales,created_at,sell_price',
            'sorting'   =>  'string|in:asc,desc',
            'keyword'   =>  'string'
        ], [
            'cat_id.string'    =>  '分类ID必须为整数',
            'cat_id.exists'     =>  '分类不存在',
            'page.integer'      =>  '当前页面必须整型',
            'name.string'       =>  '排序名称必须为字符',
            'name.in'           =>  '排序名称选择错误',
            'sorting.string'    =>  '排序方式必须为字符',
            'sorting.in'        =>  '排序方式选择错误',
            'keyword.string'    =>  '搜索关键词必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_LIST_ERROR, $validator->errors()->first());
        }

        $query['cat_id']    =   $this->request->input('cat_id', NULL);
        $query['keyword']   =   $this->request->input('keyword', '');
        $current_page       =   $this->request->input('page', 1);
        $sort_name          =   $this->request->input('name', 'sorting');
        $sort_desc          =   $this->request->input('sorting', 'desc');

        [$flag, $result]    =   $this->service->goodsList($query, $current_page, $sort_name, $sort_desc);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_LIST_ERROR, $result);
        }

    }

    /**
     * 商品详情页面
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailGoods()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_unique'   =>  'required|string',
        ], [
            'good_unique.required'  =>  '商品唯一标识符不能为空',
            'good_unique.string'    =>  '商品唯一标识符必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_DETAIL_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->goodsDetail($this->request->post('good_unique'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_DETAIL_ERROR, $result);
        }
    }

    /**
     * 商品评价内容
     * @return \Illuminate\Http\JsonResponse
     */
    public function commentGoods()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'required|integer|exists:mini_product,id',
            'query'     =>  'array',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整型',
            'good_id.exists'    =>  '商品不存在',
            'query.array'       =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_COMMENT_ERROR, $validator->errors()->first());
        }

        $good_id    =   $this->request->post('good_id');
        $good_query =   $this->request->post('query', []);
        $good_page  =   $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->goodsComment((int) $good_id, $good_query, (int) $good_page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_COMMENT_ERROR, $result);
        }
    }

    /**
     * 热门商品推荐
     * @return \Illuminate\Http\JsonResponse
     */
    public function hotGoods()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'integer|exists:mini_product,id',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整型',
            'good_id.exists'    =>  '商品不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_COMMENT_ERROR, $validator->errors()->first());
        }

        $good_id    =   $this->request->post('good_id', 0);
        $good_page  =   $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->goodsHot((int) $good_id, (int) $good_page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_COMMENT_ERROR, $result);
        }
    }

    /**
     * 商品详情海报
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function posterGoods()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'integer|exists:mini_product,id',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整型',
            'good_id.exists'    =>  '商品不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_MATERIAL_ERROR, $validator->errors()->first());
        }

        $good_id    =   $this->request->post('good_id', 0);
        [$flag, $result]    =   $this->service-> goodsPoster((int) $good_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_MATERIAL_ERROR, $result);
        }
    }

    /**
     * 商品发圈素材
     * @return \Illuminate\Http\JsonResponse
     */
    public function materialGoods()
    {
        $validator  =   Validator::make($this->request->post(), [
            'good_id'   =>  'required|integer|exists:mini_product,id',
        ], [
            'good_id.required'  =>  '商品ID不能为空',
            'good_id.integer'   =>  '商品ID必须为整型',
            'good_id.exists'    =>  '商品不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_GOODS_MATERIAL_ERROR, $validator->errors()->first());
        }

        $good_id    =   $this->request->post('good_id', 0);
        $good_page  =   $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->goodsMaterial((int) $good_id, (int) $good_page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_GOODS_MATERIAL_ERROR, $result);
        }
    }

}
